package com.ldc.common.utils;

import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 功能说明
 *
 * @auther liudc
 * @since 2023/6/14
 */
public class ApiResponseUtils {

    public ApiResponseUtils() {
        throw new IllegalStateException("非法创建");
    }

    public static <T> void writeObject(HttpServletResponse response, T data) throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter printWriter = response.getWriter();
        ObjectMapper mapper = new ObjectMapper();
        final String json = mapper.writeValueAsString(data);
        printWriter.write(json);
        printWriter.close();
    }
}
